﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace Sistema.Util
{
    public class FTP
    {
        public FTP()
        {
        }

       public void Enviar(String url, String diretorio,String arquivo, String login, String senha)
        {
            try
            {

                FileStream stream = File.OpenRead(arquivo);
                byte[] buffer = new byte[stream.Length];

                stream.Read(buffer, 0, buffer.Length);

                Uri uri = new Uri(url + "/" + diretorio+arquivo); // o bla.xml vai para a pasta teste do user logado \o/
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uri);
                request.Credentials = new NetworkCredential(login, senha);
                request.KeepAlive = false;
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.UseBinary = true;
                request.ContentLength = buffer.Length;



                Stream strm = request.GetRequestStream();
                strm.Write(buffer, 0, buffer.Length);
                strm.Close();
                stream.Close();


            }
            catch
            {
                throw new Exception();
            }
        }


       public void Enviar(String url,String arquivo, String login, String senha)
       {
           try
           {

               FileStream stream = File.OpenRead(arquivo);
               byte[] buffer = new byte[stream.Length];

               stream.Read(buffer, 0, buffer.Length);

               Uri uri = new Uri(url+arquivo); // o bla.xml vai para a pasta teste do user logado \o/
               FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uri);
               request.Credentials = new NetworkCredential(login, senha);
               request.KeepAlive = false;
               request.Method = WebRequestMethods.Ftp.UploadFile;
               request.UseBinary = true;
               request.ContentLength = buffer.Length;



               Stream strm = request.GetRequestStream();
               strm.Write(buffer, 0, buffer.Length);
               strm.Close();
               stream.Close();


           }
           catch
           {
               throw new Exception();
           }
       }

       public void Enviar(String url,String diretorio, IList<String> arquivo, String login, String senha)
       {
           try
           {
               
               for (int i = 0; i < arquivo.Count; i++)
               {
                   FileStream stream = File.OpenRead(arquivo[i]);
                   byte[] buffer = new byte[stream.Length];
                   stream.Read(buffer, 0, buffer.Length);

                   Uri uri = new Uri(url+diretorio+arquivo[i]); 
                   FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uri);
                   request.Credentials = new NetworkCredential(login, senha);
                   request.KeepAlive = false;
                   request.UseBinary = true;
                   request.Method = WebRequestMethods.Ftp.UploadFile;
               
                   request.ContentLength = buffer.Length;

                   Stream strm = request.GetRequestStream();
                   strm.Write(buffer, 0, buffer.Length);
                   strm.Close();
                   stream.Close();
                   
               }
                   
           }
           catch
           {
               throw new Exception();
           }
       }


     }

}
